package com.unitedvision.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityNotFoundException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.unitedvision.core.domain.CompanyBillingStatus;
import com.unitedvision.core.domain.Status;
import com.unitedvision.persistence.domain.CompanyBillEntity;
import com.unitedvision.persistence.domain.CompanyEntity;
import com.unitedvision.persistence.domain.CompanyModuleEntity;
import com.unitedvision.persistence.domain.CustomerEntity;
import com.unitedvision.persistence.domain.ModuleEntity;
import com.unitedvision.persistence.domain.PeriodeValue;
import com.unitedvision.persistence.repository.CompanyBillRepository;
import com.unitedvision.persistence.repository.CompanyModuleRepository;
import com.unitedvision.persistence.repository.CompanyRepository;
import com.unitedvision.persistence.repository.CustomerRepository;
import com.unitedvision.service.CompanyService;

@Service
public class CompanyServiceImpl implements CompanyService {

	@Resource
	private CustomerRepository customerRepository;
	@Resource
	private CompanyRepository companyRepository;
	@Resource
	private CompanyModuleRepository companyModuleRepository;
	@Resource
	private CompanyBillRepository companyBillRepository;

	@Transactional
	public void save(CompanyEntity companyEntity) {
		companyRepository.save(companyEntity);
	}
	
	@Override
	public void add(CustomerEntity customer, String description) {
		CompanyEntity companyEntity = new CompanyEntity(customer, description, new Date(), Status.NONACTIVE);

		save(companyEntity);
	}

	@Override
	public void update(int id, CustomerEntity customer, String description) {
		CompanyEntity company = companyRepository.getOne(id);
		company.setCustomer(customer);
		company.setDescription(description);

		save(company);
	}

	@Override
	@Transactional
	public void delete(CompanyEntity companyEntity) {
		companyRepository.delete(companyEntity);
	}

	@Override
	public CompanyEntity get(int id) {
		return companyRepository.findOne(id);
	}

	@Override
	public List<CompanyEntity> getAll() {
		return companyRepository.findAll();
	}

	@Override
	public CompanyEntity getOneByCustomer(CustomerEntity customerEntity) throws EntityNotFoundException {
		return companyRepository.findByCustomer(customerEntity);
	}

	@Override
	public List<CompanyEntity> getByStatus(Status status) {
		return companyRepository.findByStatus(status);
	}

	@Override
	public void activateCompany(CompanyEntity companyEntity) {
		if (companyEntity == null)
			throw new NullPointerException();
		
		companyEntity.setStatus(Status.ACTIVE);
		save(companyEntity);
	}

	@Override
	public void deactivateCompany(CompanyEntity companyEntity) {
		if (companyEntity == null)
			throw new NullPointerException();
		
		companyEntity.setStatus(Status.NONACTIVE);
		save(companyEntity);
	}

	@Override
	public long count() {
		return companyRepository.count();
	}

	@Override
	public void save(CompanyModuleEntity companyModule) {
		companyModuleRepository.save(companyModule);
	}

	@Override
	public void delete(CompanyModuleEntity companyModule) {
		companyModuleRepository.delete(companyModule);
	}

	@Override
	public CompanyModuleEntity getCompanyModule(int id) {
		return companyModuleRepository.findOne(id);
	}

	@Override
	public List<CompanyModuleEntity> getCompanyModuleByModule(ModuleEntity module) {
		return companyModuleRepository.findByModule(module);
	}

	@Override
	public List<CompanyModuleEntity> getCompanyModuleByCompany(CompanyEntity company) {
		return companyModuleRepository.findByCompany(company);
	}

	@Override
	public long countCompanyModule() {
		return companyModuleRepository.count();
	}

	@Override
	public void save(CompanyBillEntity companyBill) {
		companyBillRepository.save(companyBill);
	}

	@Override
	public void delete(CompanyBillEntity companyBill) {
		companyBillRepository.delete(companyBill);
	}

	@Override
	public CompanyBillEntity getBill(int id) {
		return companyBillRepository.findOne(id);
	}

	@Override
	public List<CompanyBillEntity> getAllBill() {
		return companyBillRepository.findAll();
	}

	@Override
	public List<CompanyBillEntity> getBillByCompany(CompanyEntity company) {
		return companyBillRepository.findByCompany(company);
	}

	@Override
	public List<CompanyBillEntity> getBillByStatus(CompanyBillingStatus status) {
		return companyBillRepository.findByStatus(status);
	}

	@Override
	public List<CompanyBillEntity> getBillByPeriode(PeriodeValue periode) {
		return companyBillRepository.findByPeriode(periode);
	}

	@Override
	public long countBill() {
		return companyBillRepository.count();
	}
}
